package org.marsdonne.auth;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.marsdonne.archives.Client;
import org.marsdonne.archives.OrganizationType;
import org.marsdonne.archives.Platform;
import org.marsdonne.commons.BaseEntity;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"client", "platform", "identifier"}))
@NoArgsConstructor
public class EndPointUser extends BaseEntity {

    @ManyToOne
    private PlatformUser platformUser;

    @ManyToOne
    private UnionUser unionUser;

    @Column(nullable = false)
    private Client client;

    @Column(nullable = false)
    private Platform platform;

    @Column(nullable = false)
    private String identifier; //e.g. wechat openId

    @Column(nullable = false)
    private String secretCode; //e.g. wechat session_key / SMS verification code

    @Column(nullable = false)
    private String nickname;//e.g. wechat nickname or alipay member name

    @Column
    private String avatarUrl;

    public EndPointUser(Client client, Platform platform, String identifier, String secretCode, String nickname) {
        this.client = client;
        this.identifier = identifier;
        this.secretCode = secretCode;
        this.platform = platform;
        this.nickname = nickname;
    }

    public boolean isNew() {
        return new Date().getTime() - this.dateCreated.getTime() < 1000 * 10;
    }
}
